// 加
export function accAdd(num1, num2) {
  var r1, r2, m, c
  try {
    r1 = num1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = num2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  c = Math.abs(r1 - r2)
  m = Math.pow(10, Math.max(r1, r2))
  if (c > 0) {
    var cm = Math.pow(10, c)
    if (r1 > r2) {
      num1 = Number(num1.toString().replace('.', ''))
      num2 = Number(num2.toString().replace('.', '')) * cm
    } else {
      num1 = Number(num1.toString().replace('.', '')) * cm
      num2 = Number(num2.toString().replace('.', ''))
    }
  } else {
    num1 = Number(num1.toString().replace('.', ''))
    num2 = Number(num2.toString().replace('.', ''))
  }
  return (num1 + num2) / m
}
// 减
export function accSub(num1, num2) {
  var r1, r2, m, n
  try {
    r1 = num1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = num2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2)) // last modify by deeka //动态控制精度长度
  n = r1 >= r2 ? r1 : r2
  return ((num1 * m - num2 * m) / m).toFixed(n)
}
// 除法
export function accMul(num1, num2) {
  var m = 0
  var s1 = num1.toString()
  var s2 = num2.toString()
  try {
    m += s1.split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  try {
    m += s2.split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m)
}
// 乘法
export function accDiv(num1, num2) {
  var t1 = 0
  var t2 = 0
  var r1
  var r2
  try {
    t1 = num1.toString().split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  try {
    t2 = num2.toString().split('.')[1].length
  } catch (e) {
    console.log(e)
  }

  r1 = Number(num1.toString().replace('.', ''))
  r2 = Number(num2.toString().replace('.', ''))
  return (r1 / r2) * Math.pow(10, t2 - t1)
}
// 判断是否是数字
function IsNumber(value) {
  return typeof value === 'number' && !isNaN(value)
}
function toNonExponential(num) {
  var m = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/)
  return num.toFixed(Math.max(0, (m[1] || '').length - m[2]))
}
export function floatFix(num, n = 2, afterStr = null, round = false, forceFix = false) {
  // 强制n位小数（返回字符窜类型）
  if (IsNumber(num)) {
    num = Number(num)
    let fix = '1'
    for (var i = 0; i < n; i++) {
      fix += '0'
    }
    fix = Number(fix) // 以上根据n循环出因数并转成数字
    if (round) {
      // 四舍五入
      num = Math.round(num * fix) / fix
    } else {
      // 无需四舍五入
      // let float=parseFloat(num).toFixed(n+1) //此方法对于999.999类似无法精确
      // Math.trunc()
      const float = toNonExponential(parseFloat(num)).toString()
      if (float.includes('.')) {
        // 判断是否有小数点
        num = Number(float.substring(0, float.indexOf('.') + n + 1))
      } else {
        num = Number(float)
      }
      // num = parseInt(num * 10000/10000*fix) / fix; //此方法有弱精度问题，如4.56
    }
    if (forceFix) {
      num = num.toString()
      const numArr = num.split('.')
      if (numArr.length > 1) {
        const arrS = numArr[1].toString()
        for (let i = 0; i < n - arrS.length; i++) {
          num += '0'
        }
      } else {
        num += '.'
        for (let i = 0; i < n; i++) {
          num += '0'
        }
      }
      num = Number(num)
    }
  }

  // return (Math.floor(num*fix)/fix).toFixed(n);	//*因数，舍小数，除因数，不足位补0
  if (IsNumber(num)) {
    return afterStr ? num + ' ' + afterStr : num
  } else return '--'
}
// 数字类型折算
export function convertData(value, rounding = 2, unit = '', forceFix = false) {
  let newValue = ''
  if (IsNumber(value)) {
    if (Number(value) >= 1000000) {
      newValue = rounding !== null ? floatFix(value / 1000000, rounding, null, false, forceFix) : accMul(value, 1000000)
      return [newValue + 'M' + unit, newValue, 'M' + unit]
    } else if (Number(value) >= 10000) {
      newValue = rounding !== null ? floatFix(value / 1000, rounding, null, false, forceFix) : accMul(value, 1000)
      return [newValue + 'K' + unit, newValue, 'K' + unit]
    } else {
      newValue = rounding !== null ? floatFix(value, rounding, null, false, forceFix) : value
      return [newValue + ' ' + unit, newValue, origin + unit]
    }
  }
}
